package org.esti.model.dao.impl;

import org.esti.model.Interview;
import org.esti.model.InterviewResult;
import org.esti.model.dao.InterviewDAO;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author Artem Loginov (logart)
 */
@Repository
public class InterviewDAOimpl implements InterviewDAO {

    @PersistenceContext
    private EntityManager entityManager;

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Interview> getUserInterviews(long userId) {

        Query query = entityManager.createQuery(
                "FROM Interview WHERE user_id=:userId"
        );
        query.setParameter("userId", userId);
        return (List<Interview>) query.getResultList();

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Interview get(long interviewId) {
        Query query = entityManager.createQuery(
                "FROM Interview WHERE id=:interviewId"
        );
        query.setParameter("interviewId", interviewId);
        return (Interview) query.getSingleResult();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<InterviewResult> getInterviewResult(long interviewId) {
        Query query = entityManager.createQuery(
                "FROM InterviewResult WHERE interview_id=:interviewId"
        );
        query.setParameter("interviewId", interviewId);
        return query.getResultList();
    }
}
